package cn.itsource.pethome.basic.controller;

import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.FastDfsApiOpr;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/common")
public class FileUploadController {

    @PostMapping("/fastDfs")
    public AjaxResult fastDfs(MultipartFile file){
        try {
            //获取文件真实名字
            String filename = file.getOriginalFilename();
            //获取文件后缀名
            String ext = FilenameUtils.getExtension(filename);
            //上传后的名字
            String upload = FastDfsApiOpr.upload(file.getBytes(), ext);
            return AjaxResult.me().setResultObj(upload);
        } catch (IOException e) {
            e.printStackTrace();
            return new AjaxResult("上传成功" + e.getMessage());
        }

    }

    @DeleteMapping
    public AjaxResult del(@RequestParam(required = true,value = "path") String path){
        String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
        String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
        String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
        System.out.println(groupName);
        System.out.println(remotePath);
        FastDfsApiOpr.delete(groupName, remotePath);
        return  AjaxResult.me();
    }

}
